package vn.com.aptech.msh.controller;

import java.io.Serializable;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import vn.com.aptech.msh.business.IEnquiryBean;

import vn.com.aptech.msh.entities.Enquiry;
import vn.com.aptech.msh.entities.User;
import vn.com.aptech.msh.utils.NavigationHelper;
import vn.com.aptech.msh.utils.ResourceBundleHelper;

/**
 *
 * @author trietto
 */
@Named("enquiryFormController")
@RequestScoped
public class EnquiryFormController implements Serializable {

    @Inject
    IEnquiryBean enquiryBusiness;
    @Inject
    @Named("currentUser")
    User currentUser;
    private Enquiry model;

    @PostConstruct
    public void init() {
        prepareData();
    }

    public void prepareData() {
        model = new Enquiry();
        if (currentUser != null) {
            model.setSenderName(currentUser.getFullname());
            model.setSenderEmail(currentUser.getEmail());
        }
    }

    public String send() {
        try {
            model.setStatus(0);
            model.setDate(Calendar.getInstance().getTime());
            enquiryBusiness.sendEnquiry(model);
            return NavigationHelper.toSuccessPage("enquiryForm");
        } catch (Exception ex) {
            Logger.getLogger(EnquiryFormController.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get("register.failed"), "");
            FacesContext.getCurrentInstance().addMessage("frmRegister", msg);
            return null;
        }
    }

    public Enquiry getModel() {
        return model;
    }

    public void setModel(Enquiry model) {
        this.model = model;
    }
}
